package com.matou.commons.constants;

import com.matou.commons.exception.IError;
import com.matou.commons.exception.MtError;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author chenmiao0212
 * @Date 2021/5/8 17:26
 * @Description BaseResponse 统一返回对象
 * @Version 1.0
 */
@Data
public class Resp<T> implements Serializable {
    /**  成功信息 **/
    private static final MtError MT000000 = MtError.MT_000000;

    private String code;
    private String msg;
    private T data;

    public static <T> Resp<T> success() {
        return new Resp<T>(MtError.MT_000000);
    }

    public static <T> Resp<T> success(T data) {
        return new Resp<T>(MT000000.getCode(),MT000000.getTermMsg(), data);
    }

    public static <T> Resp<T> successMsg(String msg) {
        return new Resp<T>(MT000000.getCode(), msg);
    }

    public static <T> Resp<T> success(String msg, T data) {
        return new Resp<T>(MT000000.getCode(), msg, data);
    }

    public static <T> Resp<T> failure(MtError error) {
        return new Resp<T>(error.getCode(), error.getTermMsg(), null);
    }

    public static <T> Resp<T> failure(String code, String msg) {
        return new Resp<T>(code, msg, null);
    }

    public boolean isSuccess() {
        return MtError.MT_000000.getCode().equals(this.code);
    }

    public Resp() {
    }

    public Resp(IError error) {
        this.code = error.getCode();
        this.msg = error.getErrorMsg();
    }

    public Resp(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Resp(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
